package com.web.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * Created by reeves on 2015/7/6.
 *
 * 注：PageSecondType 作为一个Model，但是数据库中没有与之对应的表或视图，属于抽象的表，因为这个Model里面数据操作是使用Db来做的
 */
public class PageSecondType extends Model<PageSecondType>{
    public static final PageSecondType pageDao = new PageSecondType();

    /**
     * 获取二级分类分页数据
     *
     * @param pageNumber
     * @param pageSize
     * @param secondType
     * @return
     */
    public Page<Record> getSecondPage(int pageNumber, int pageSize, int secondType) {
        return Db.paginate(pageNumber, pageSize, "SELECT page_edit.id,pageDate,pageType,editorId,name,imageSrc,firstTitle,editTypeId,thirdTypeName,recomPos,pageFrom,isReady,sourceAddress",
                "FROM page_edit,page_titleimage,type_info,com_userinfo WHERE page_edit.titleImage=page_titleimage.imageId AND page_edit.editorId=com_userinfo.id AND page_edit.typeId=type_info.id AND secondType=? ORDER BY pageDate DESC,uploadDate DESC,isReady ASC,typeId ASC", secondType);

    }

    /**
     * 获取二级分类分页总数
     * 获取二级分类页数：1. 信息条数是今天上传的条数；
     *
     * @param pageSize
     * @param secondType
     * @return
     */
    public int getPageNumberSecond(int pageSize, int secondType) {
        Long countLong = Db.queryLong("SELECT COUNT(*) FROM page_edit,type_info WHERE page_edit.typeId=type_info.id AND secondType=?", secondType);
        int count = countLong.intValue();
        return count % pageSize == 0 ? (count / pageSize) : (count / pageSize + 1);
    }
}
